package com.dzt.androidframework.database.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dzt.androidframework.database.javabean.Task;
import com.dzt.androidframework.express.javabean.OrderInfoLitePal;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by M02323 on 2017/6/10.
 */

public class TaskDao {
	private SQLiteDatabase db;
	private SQLiteHelper dbHelper;

	private TaskDao() {
	}

	private static class SingletonHolder {
		static TaskDao sInstance = new TaskDao();
	}

	public static TaskDao getInstance() {
		return SingletonHolder.sInstance;
	}

	public void init(Context context) {
		dbHelper = new SQLiteHelper(context);
		db = dbHelper.getWritableDatabase();
	}

	public long saveTask(Task task) {
		ContentValues values = new ContentValues();
		values.put(SQL.TITLE, task.getTitle());
		values.put(SQL.DESCRIPTION, task.getDescription());
		values.put(SQL.COMPLETED, task.isCompleted());
		return db.insert(SQL.TB_TASKS, null, values);
	}

	public int clearCompletedTasks() {
		String selection = SQL.COMPLETED + " LIKE ?";
		String[] selectionArgs = {"1"};
		return db.delete(SQL.TB_TASKS, selection, selectionArgs);
	}

	public int deleteTask(String taskId) {
		String selection = SQL.ID + " LIKE ?";
		String[] selectionArgs = {taskId};
		return db.delete(SQL.TB_TASKS, selection, selectionArgs);
	}

	public int deleteAll() {
		return db.delete(SQL.TB_TASKS, null, null);
	}

	public int updateTaskStatus(Task task, boolean isCompleted) {
		ContentValues values = new ContentValues();
		values.put(SQL.COMPLETED, isCompleted);
		String selection = SQL.ID + " LIKE ?";
		String[] selectionArgs = {task.getId()};
		return db.update(SQL.TB_TASKS, values, selection, selectionArgs);
	}

	public int updateTask(Task task) {
		ContentValues values = new ContentValues();
		values.put(SQL.TITLE, task.getTitle());
		values.put(SQL.DESCRIPTION, task.getDescription());
		String selection = SQL.ID + " LIKE ?";
		String[] selectionArgs = {task.getId()};
		return db.update(SQL.TB_TASKS, values, selection, selectionArgs);
	}

	public Task findById(String taskId) {
		String[] projection = SQL.allTaskColumns;
		String selection = SQL.ID + " LIKE ?";
		String[] selectionArgs = {taskId};

		Cursor cursor = db.query(
				SQL.TB_TASKS, projection, selection, selectionArgs, null, null, null);
		if (cursor == null)
			return null;
		Task task = null;
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			String id = cursor.getString(cursor.getColumnIndex(SQL.ID));
			String t = cursor.getString(cursor.getColumnIndex(SQL.TITLE));
			String des = cursor.getString(cursor.getColumnIndex(SQL.DESCRIPTION));
			boolean com = cursor.getInt(cursor.getColumnIndex(SQL.COMPLETED)) == 1;
			task = new Task(t, des, id, com);
		}
		cursor.close();
		return task;
	}

	public List<Task> findAll() {
		String[] columns = SQL.allTaskColumns;
		Cursor cursor = db.query(SQL.TB_TASKS, columns,
				null, null, null, null, null);
		if (cursor == null)
			return null;
		List<Task> tasks = new ArrayList<>();
		while (cursor.moveToNext()) {
			String id = cursor.getString(cursor.getColumnIndex(SQL.ID));
			String title = cursor.getString(cursor.getColumnIndex(SQL.TITLE));
			String des = cursor.getString(cursor.getColumnIndex(SQL.DESCRIPTION));
			int com = cursor.getInt(cursor.getColumnIndex(SQL.COMPLETED));
			Task task = new Task(title, des, id, (com > 0));
			tasks.add(task);
		}
		cursor.close();
		return tasks;
	}

	public long saveExpress(OrderInfoLitePal order) {
		ContentValues values = new ContentValues();
		values.put(SQL.EXPRESS_NUMBER, order.getNu());
		values.put(SQL.EXPRESS_COMPANY, order.getCom());
		values.put(SQL.EXPRESS_STATE, order.getState());
		values.put(SQL.EXPRESS_TIME, order.getTime());
		values.put(SQL.EXPRESS_SELECT, order.isSelect());
		return db.insert(SQL.TB_EXPRESS, null, values);
	}

	public List<OrderInfoLitePal> findAllExpress() {
		String[] columns = SQL.allExpressColumns;
		Cursor cursor = db.query(SQL.TB_EXPRESS, columns,
				null, null, null, null, null);
		if (cursor == null)
			return null;
		List<OrderInfoLitePal> orders = new ArrayList<>();
		while (cursor.moveToNext()) {
			String id = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_ID));
			String number = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_NUMBER));
			String company = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_COMPANY));
			String state = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_STATE));
			String time = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_TIME));
			int isSelect = cursor.getInt(cursor.getColumnIndex(SQL.EXPRESS_SELECT));
			OrderInfoLitePal order = new OrderInfoLitePal();
			order.setId(Integer.parseInt(id));
			order.setNu(number);
			order.setCom(company);
			order.setState(state);
			order.setTime(time);
			order.setIsSelect((isSelect > 0));
			orders.add(order);
		}
		cursor.close();
		return orders;
	}

	public List<OrderInfoLitePal> findSignedExpress() {
		String[] columns = SQL.allExpressColumns;
		String selection = SQL.EXPRESS_STATE + " LIKE ?";
		String[] selectionArgs = {"3"};
		Cursor cursor = db.query(SQL.TB_EXPRESS, columns,
				selection, selectionArgs, null, null, null);
		if (cursor == null)
			return null;
		List<OrderInfoLitePal> orders = new ArrayList<>();
		while (cursor.moveToNext()) {
			String id = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_ID));
			String number = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_NUMBER));
			String company = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_COMPANY));
			String state = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_STATE));
			String time = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_TIME));
			int isSelect = cursor.getInt(cursor.getColumnIndex(SQL.EXPRESS_SELECT));
			OrderInfoLitePal order = new OrderInfoLitePal();
			order.setId(Integer.parseInt(id));
			order.setNu(number);
			order.setCom(company);
			order.setState(state);
			order.setTime(time);
			order.setIsSelect((isSelect > 0));
			orders.add(order);
		}
		cursor.close();
		return orders;
	}

	public List<OrderInfoLitePal> findUNSignedExpress() {
		String[] columns = SQL.allExpressColumns;
		String selection = SQL.EXPRESS_STATE + " NOT LIKE ?";
		String[] selectionArgs = {"3"};
		Cursor cursor = db.query(SQL.TB_EXPRESS, columns,
				selection, selectionArgs, null, null, null);
		if (cursor == null)
			return null;
		List<OrderInfoLitePal> orders = new ArrayList<>();
		while (cursor.moveToNext()) {
			String id = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_ID));
			String number = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_NUMBER));
			String company = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_COMPANY));
			String state = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_STATE));
			String time = cursor.getString(cursor.getColumnIndex(SQL.EXPRESS_TIME));
			int isSelect = cursor.getInt(cursor.getColumnIndex(SQL.EXPRESS_SELECT));
			OrderInfoLitePal order = new OrderInfoLitePal();
			order.setId(Integer.parseInt(id));
			order.setNu(number);
			order.setCom(company);
			order.setState(state);
			order.setTime(time);
			order.setIsSelect((isSelect > 0));
			orders.add(order);
		}
		cursor.close();
		return orders;
	}

	public int updateExpress(OrderInfoLitePal order) {
		ContentValues values = new ContentValues();
		values.put(SQL.EXPRESS_STATE, order.getState());
		values.put(SQL.EXPRESS_TIME, order.getTime());
		String selection = SQL.EXPRESS_ID + " LIKE ?";
		String[] selectionArgs = {String.valueOf(order.getId())};
		return db.update(SQL.TB_EXPRESS, values, selection, selectionArgs);
	}

	public void close() {
		db.close();
		dbHelper.close();
	}
}
